<?php

namespace Wap\Controller;

use User\Model\FactoryModel;
use Wap\Service\WeixinService;

class JTController extends WapController
{

    public $user;
    public $wxuser;

    public function __construct()
    {
        parent::__construct();
        $this->wxuser = $wx = WeixinService::getWxuser("iequde1415960341");

        $wxJSApi = WeixinService::buildWXJSApi($wx);
        $wxConfig = array(
            'appId' => $wx['appid'],
            'timestamp' => $wxJSApi['timestamp'],
            'nonceStr' => $wxJSApi['noncestr'],
            'signature' => $wxJSApi['sha1']
        );
        $this->assign('wxConfig', $wxConfig);
        if (!headers_sent() && // 如果页面头部信息还没有输出--
            extension_loaded("zlib") && // 而且php已经加载了zlib扩展--
            strstr($_SERVER["HTTP_ACCEPT_ENCODING"], "gzip")
        ) //而且浏览器接受GZIP--
        {
            ini_set('zlib.output_compression', 'On');
            ini_set('zlib.output_compression_level', '4');
        }
    }

    /*
    *静默授权url生成
    */
    private function handleCriteriaURL()
    {
//        $this->user = M("jt_user")->where(["id" => 1])->find();return;
        $this->user = WeixinService::getUser();
        if (empty($this->user)) {
            if (!isset($_GET['code'])) {
                $url = C('SITE_URL') . $_SERVER['REQUEST_URI'];
                $url = WeixinService::buildOauthURI($url, $this->wxuser['appid']);
                header("Location: " . $url);
                exit();
            } else {
                if (isset($_GET['code'])) {
                    WeixinService::setUser($this->wxuser['appid'], $this->wxuser['appsecret'], $_GET['code']);
                    $this->user = WeixinService::getUser();
                    if (empty($this->user)) {
                        $url = C('SITE_URL') . $_SERVER['REQUEST_URI'];
                        $url = WeixinService::buildOauthURI($url, $this->wxuser['appid']);
                        header("Location: " . $url);
                        exit();
                    }
                }
            }
        }
    }

    /*
    *微信用户
    */
    private function handleCriteriaUser()
    {

    }

    private function getTitleContent()
    {
        $id = I("get.id", "", "");
        //----获取子栏目---
        $cond_son['parent_id'] = $id;
        $cond_son['status'] = array("NEQ", 9);
        $sons = M("jt_title")->where($cond_son)->order("priority asc")->select();

        //---获取新闻内容|获取banner内容---
        $news = $banners = [];
        if (!empty($sons)) {//--有子栏目则获取子栏目内容--
            $cond_news['ttl_id'] = $sons[0]['id'];
            $cond_news['status'] = array("IN", [1, 2]);
            $news = M("jt_news")->where($cond_news)->order("status desc,update_time desc")->limit(0, 10)->select();
        } else {
            $cond_news['pttl_id'] = $id;
            $cond_news['status'] = array("IN", [1, 2]);
            $news = M("jt_news")->where($cond_news)->order("status desc,update_time desc")->limit(0, 10)->select();
        }
        $cond_banner['title_id'] = $id;
        $cond_banner['status'] = array("NEQ", 9);
        $banners = M("jt_title_banner")->where($cond_banner)->order("priority desc,add_time desc")->limit(0, 5)->select();

        $cond_title['id'] = $id;
        $title_name = M("jt_title")->where($cond_title)->getField("name");
        $res = ["sons" => $sons, "news" => $news, "banners" => $banners, "title_name" => $title_name];
        return $res;
    }

    public function index()
    {
        $this->handleCriteriaURL();
        $this->handleCriteriaUser();
        $id = I("get.id", "", "");

        //---非游客跳转至登录---
        $visitor = I("get.visitor", "", "");
        $wxuser = $this->user;
        $cond['openid'] = $wxuser['openid'];
        $cond['status'] = array("NEQ", 9);
        $user = M("jt_user")->where($cond)->find();
        if (empty($user) && empty($visitor)) {
            $url = C('SITE_URL') . "/Wap/JT/login?id=" . $id;
            header("Location:" . $url);
            exit;
        }
//        $res = $this->getTitleContent();
//        $this->assign("res", json_encode($res));
        $this->display();
    }

    public function ajaxTitleContent()
    {
        $res = $this->getTitleContent();
        $this->ajaxReturn(["res" => $res]);
    }

    public function ajaxTitleContentMore()
    {
        $id = I("get.title_id", "", "");

        $cond_news['type'] = 0;
        $cond_news['status'] = array("IN", [1, 2]);

        $cond_title['id'] = $id;
        $title = M("jt_title")->where($cond_title)->find();
        if ($title['level'] == 1) {//---子栏目---
            $cond_news['ttl_id'] = $id;
        } else {
            $cond_news['pttl_id'] = $id;
        }

        $start_date = I("get.start_date", "", "");
        $end_date = I("get.end_date", "", "");
        $title = I("get.title", "", "");

        if (!empty($start_date)) {
            $start_date = $start_date . " 00.00.00";
            if (empty($end_date)) {
                $end_date = date("Y-m-d") . " 23:59:59";
            }
            $cond_news['add_time'] = array("between", [$start_date, $end_date]);
        } elseif (!empty($end_date)) {
            $end_date = $end_date . " 23:59:59";
            $cond_news['add_time'] = array("ELT", $end_date);
        }
        if (!empty($title)) {
            $cond_news['title'] = array("LIKE", "%" . $title . "%");
        }
        $page = I("get.page", 1, "");

        $news = M("jt_news")->where($cond_news)->order("status desc,update_time desc")->limit(($page - 1) * 10, 10)->select();
        $this->ajaxReturn(["res" => $news]);
    }

    private function getDepts()
    {
        $cond['status'] = array("NEQ", 9);
        $depts = M("jt_dept")->where($cond)->order("priority asc")->select();
        return $depts;
    }

    public function login()
    {
        $this->handleCriteriaURL();
        $this->handleCriteriaUser();
//        $depts = $this->getDepts();
//        $this->assign("res", json_encode($depts));
        $this->display();
    }

    public function ajaxGetDepts()
    {
        $wxuser = $this->user;
        $depts = $this->getDepts();
        $this->ajaxReturn(["res" => $depts, "user" => $wxuser]);
    }

    //--调用交投接口确认是否为交投员工---
    private function checkJtUser($phone)
    {
        if (($phone == "15068881637") || ($phone == "15858173759" || ($phone == "15268584995"))) {
            return 1;
        }
        $check = 0;
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, "http://mobile.cncico.com:9483/apis/getToken");
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 500);
        curl_setopt($ch, CURLOPT_COOKIEFILE, $this->lastCookieFile);
        $post_data = array("loginName" => "jtgzh", "password" => "21xIUwXJuHzaOSEb4i8q7w==");
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $out = curl_exec($ch);
        curl_close($ch);
        $content = json_decode($out);
        foreach ($content as $key => $val) {
            if ($key === "result") {//----result记录tokehttp://mobile.cncico.com:9483n信息---
                $ch1 = curl_init();
                $url = "http://mobile.cncico.com:9483/apis/api/zb/phone?mobile=" . $phone;
                curl_setopt($ch1, CURLOPT_URL, $url);
                curl_setopt($ch1, CURLOPT_HTTPGET, true);
                curl_setopt($ch1, CURLOPT_RETURNTRANSFER, 1);
                $headers = [
                    "Authorization:" . $val
                ];
                curl_setopt($ch1, CURLOPT_HTTPHEADER, $headers);
                $out1 = curl_exec($ch1);
                curl_close($ch1);
                $content1 = json_decode($out1);

                foreach ($content1 as $key1 => $val1) {
                    if ($key1 === "result") {
                        $check = 1;
                    }
                }

            }
        }
        return $check;
    }

    public function ajaxLoginSms()
    {
        $session_time = session("mt_time");
        if ($session_time != null && ($session_time + 30) > time()) {
            $result['code'] = -4;
            $result['msg'] = "操作太频繁";
            return $this->ajaxReturn($result);
        }
        session("mt_time", time());
        $phone = I("post.phone", "", "");
        if (empty($phone)) {
            $result['code'] = -1;
            $result['msg'] = "手机号码不能为空";
            return $this->ajaxReturn($result);
        }

        $check = $this->checkJtUser($phone);
        if (!$check) {
            $result['code'] = -3;
            $result['msg'] = "查无此人";
            return $this->ajaxReturn($result);
        }

        $cond['phone'] = $phone;
        $cond['status'] = array("NEQ", 9);
        $user = M("jt_user")->where($cond)->find();
        if (!empty($user)) {
            $result['code'] = -2;
            $result['msg'] = "手机号码已经注册过了";
            return $this->ajaxReturn($result);
        }

        $authcode = '';
        for ($i = 0; $i < 4; $i++) {
            $randAsciiNumArray = array(rand(48, 57), rand(65, 90));
            $randAsciiNum = $randAsciiNumArray [rand(0, 1)];
            $randStr = chr($randAsciiNum);
            $authcode .= $randStr;
        }
        $msg = "您的验证码为：" . $authcode;
        session("sms", $phone . $authcode);
        $res = FactoryModel::sendMsg($phone, $msg);

        $result['code'] = 1;
        $result['msg'] = "发送成功";
        return $this->ajaxReturn($result);
    }

    public function ajaxLogin()
    {
        $phone = I("post.phone", "", "");
        if (empty($phone)) {
            $result['code'] = -1;
            $result['msg'] = "手机号码不能为空";
            return $this->ajaxReturn($result);
        }
        $cond['phone'] = $phone;
        $cond['status'] = array("NEQ", 9);
        $user = M("jt_user")->where($cond)->find();
        if (!empty($user)) {
            $result['code'] = -2;
            $result['msg'] = "手机号码已经注册过了";
            return $this->ajaxReturn($result);
        }
        $sms = I("post.sms", "", "");
        if (empty($sms)) {
            $result['code'] = -3;
            $result['msg'] = "验证码不能为空";
            return $this->ajaxReturn($result);
        }
        $check_sms = $phone . $sms;
        $session_sms = session("sms");
        if ($check_sms != $session_sms) {
            $result['code'] = -4;
            $result['msg'] = "验证码不正确";
            return $this->ajaxReturn($result);
        }
        $this->handleCriteriaURL();
        $wxuser = $this->user;
        $cond_open['openid'] = $wxuser['openid'];
        $cond_open['status'] = array("NEQ", 9);
        $user = M("jt_user")->where($cond_open)->find();
        if (!empty($user)) {
            $result['code'] = -5;
            $result['msg'] = "微信号已经注册过了";
            return $this->ajaxReturn($result);
        }

        $dept_id = I("post.dept_id", "", "");
        if (empty($dept_id)) {
            $result['code'] = -6;
            $result['msg'] = "请选择所属公司";
            return $this->ajaxReturn($result);
        }

        $name = I("post.name", "", "");
        if (empty($name)) {
            $result['code'] = -7;
            $result['msg'] = "请输入真实姓名";
            return $this->ajaxReturn($result);
        }
        //----新增用户---
        $data['openid'] = $wxuser['openid'];
        $data['dept_id'] = I("post.dept_id", "", "");
        $data['name'] = $name;
        $data['phone'] = $phone;
        $user_id = M("jt_user")->add($data);

        $data_unit['user_id'] = $user_id;
        $data_unit['clock'] = 0;
        $data_unit['type'] = 0;
        $data_list [] = $data_unit;
        $data_unit['type'] = 1;
        $data_list [] = $data_unit;
        M("jt_user_clock_total")->addAll($data_list);

        $cond_dept_number_add['id'] = I("post.dept_id", "", "");
        M("jt_dept")->where($cond_dept_number_add)->setInc("reg_number");

        $result['code'] = 1;
        $result['msg'] = "注册成功";
        return $this->ajaxReturn($result);
    }

    //-----阅读打卡---
    private function getNewsContent($user = '', $first = 0)
    {
        $id = I("get.news_id", "", "");
        $visitor = I("get.visitor", "", "");

        $cond['id'] = $id;
        $cond['status'] = array("NEQ", 9);
        $news = M("jt_news")->where($cond)->find();
        if (empty($news)) {
            return $news;
        }
        //---判断是否已经有打卡记录---
        $clock = S("user_id_clock" . $user['id']);
        $time = time();
        if (($time <= $clock) || ($first == 1) || ($news['status'] == 0)) {
            return $news;
        }

        S("user_id_clock" . $user['id'], $time + 3);
        if ($visitor) {

        } else {
            if (($news['clock'] == 1)) {
                $date = date("Y-m-d H:i:s");

                //----防止规律打卡，微信阅读原文添加token码--
                if (($news['add_time'] > "2020-08-12 23:59:59") && ($news['type'] == 1)) {
                    $token = I("get.token", "", "");
                    if ($token != md5($id)) {
                        $news['add_time'] = $news['update_time'] = "2020-07-01 00:00:00";
                        return $news;
                    }
                }
                $date_effective = date("Y-m-d H:i:s", strtotime("$date - 2 week"));
                if (($news['type'] == 0) || ($date_effective < $news['update_time'])) {//---内部文章或微信文章两周内---
                    $cond_check['user_id'] = $user['id'];
                    $cond_check['new_id'] = $news['id'];
                    $cond_check['status'] = 1;
                    $count = M("jt_user_clock")->where($cond_check)->count();
                    if (!$count) {
                        M()->startTrans();
                        //---添加个人打卡信息---
                        $data['user_id'] = $user['id'];
                        $data['new_id'] = $news['id'];
                        $data['type'] = $news['type'];
                        $res1 = M("jt_user_clock")->add($data);

                        //----更新个人打卡统计---
                        $cond_user_all['user_id'] = $user['id'];
                        $cond_user_all['type'] = $news['type'];
                        $cond_user_all['status'] = 1;
                        $user_clock_all = M("jt_user_clock_total")->where($cond_user_all)->find();
                        $res2 = M("jt_user_clock_total")->where($cond_user_all)->setInc("clock");

                        //----更新部门打卡统计----
                        $cond_dept_data['new_id'] = $news['id'];
                        $cond_dept_data['dept_id'] = $user['dept_id'];
                        $cond_dept_data['status'] = 1;
                        $res3 = M("jt_dept_data")->where($cond_dept_data)->setInc("clock");

                        M()->commit();
                    }
                }

            }
        }
        S("user_id_clock" . $user['id'], null);
        return $news;
    }


    public function clock()
    {
        $this->handleCriteriaURL();
        $this->handleCriteriaUser();
        //---非游客跳转至登录---
        $id = I("get.news_id", "", "");
        $visitor = I("get.visitor", "", "");
        $token = I("get.token", "", "");
        $user = "";
        if (!$visitor) {
            $wxuser = $this->user;
            $user = S("jt_user3" . $wxuser['openid']);
            if (empty($user)) {
                $cond['openid'] = $wxuser['openid'];
                $cond['status'] = array("NEQ", 9);
                $user = M("jt_user")->where($cond)->find();
                S("jt_user3" . $wxuser['openid'], $user);
            }

            if (empty($user)) {
                //----跳转并记录新闻ID---
                $url = C('SITE_URL') . "/Wap/JT/login?news_id=" . $id . "&token=" . $token;
                header("Location: " . $url);
                exit();
            }

        }

        $res = $this->getNewsContent($user, 1);
        if (!empty($res['url']) && ($res['type'] == 0)) {
            $this->getNewsContent($user);
            header("Location: " . $res['url']);
            exit();
        }
        $this->assign("res", $res);
        $this->display();
    }

    public function ajaxGetNewsContent()
    {
        $this->handleCriteriaURL();
        $wxuser = $this->user;
        $visitor = I("get.visitor", "", "");
        $user = "";
        if (!$visitor) {
            $user = S("jt_user3" . $wxuser['openid']);
            if (empty($user)) {
                $cond['openid'] = $wxuser['openid'];
                $cond['status'] = array("NEQ", 9);
                $user = M("jt_user")->where($cond)->find();
                S("jt_user3" . $wxuser['openid'], $user);
            }
        }
        $res = $this->getNewsContent($user);
        $this->ajaxReturn(['res' => $res]);
    }

    //---排行榜---
    private function getUserInfo($user)
    {
        //---获取用户所在公司--
        $cond_dept['id'] = $user['dept_id'];
        $dept = M("jt_dept")->where($cond_dept)->find();

        //----获取阅读打卡信息----
        $cond_nb['type'] = 0;
        $cond_nb['status'] = 1;
        $cond_nb['user_id'] = $user['id'];
        $user_total_nb = M("jt_user_clock_total")->where($cond_nb)->find();

        //----获取文章打卡信息----
        $cond_wx['type'] = 1;
        $cond_wx['status'] = 1;
        $cond_wx['user_id'] = $user['id'];
        $user_total_wx = M("jt_user_clock_total")->where($cond_wx)->find();

        //获取阅读打卡排行
        $user_id_arr = S("user_nb_clock_all");
        if (empty($user_id_arr)) {
            $cond_user_id_arr['status'] = 1;
            $cond_user_id_arr['type'] = 0;
            $user_id_arr = M("jt_user_clock_total")->where($cond_user_id_arr)->order("clock desc,user_id asc")->field("user_id")->select();
        }

        $nb_rank = 1;
        foreach ($user_id_arr as $user_id) {
            if ($user_id['user_id'] == $user['id']) {
                break;
            }
            $nb_rank++;
        }

        //获取文章打卡排行
        $user_id_arr = S("user_wx_clock_all");
        if (empty($user_id_arr)) {
            $cond_user_id_arr['status'] = 1;
            $cond_user_id_arr['type'] = 1;
            $user_id_arr = M("jt_user_clock_total")->where($cond_user_id_arr)->order("clock desc,user_id asc")->field("user_id")->select();
        }
        $wx_rank = 1;
        foreach ($user_id_arr as $user_id) {
            if ($user_id['user_id'] == $user['id']) {
                break;
            }
            $wx_rank++;
        }


        $cond_wx_max['type'] = 1;
        $cond_wx_max['clock'] = 1;
        $cond_wx_max['status'] = array("NEQ", 9);
        $wx_clock_max = M("jt_news")->where($cond_wx_max)->count();


        $cond_nb_max['type'] = 0;
        $cond_nb_max['clock'] = 1;
        $cond_nb_max['status'] = array("NEQ", 9);
        $nb_clock_max = M("jt_news")->where($cond_nb_max)->count();

        if($user_total_wx['clock'] > $wx_clock_max){
            $user_total_wx['clock'] = $wx_clock_max;
        }
        if($user_total_nb['clock'] > $nb_clock_max){
            $user_total_nb['clock'] = $nb_clock_max;
        }

        $res_user = ["name" => $user['name'], "dept_name" => $dept['name'],
            "nb_clock" => $user_total_nb['clock'], "nb_rank" => $nb_rank, "wx_clock" => $user_total_wx['clock'], "wx_rank" => $wx_rank];
        return $res_user;
    }

    public function rank()
    {
        $this->handleCriteriaURL();
        $this->handleCriteriaUser();
        //---非游客跳转至登录---
        $id = I("get.id", "", "");
        $wxuser = $this->user;
        $cond['openid'] = $wxuser['openid'];
        $cond['status'] = array("NEQ", 9);
        $user = M("jt_user")->where($cond)->find();
        if (empty($user)) {
            //----跳转并记录新闻ID---
            $url = C('SITE_URL') . "/Wap/JT/login?id=" . $id;
            header("Location: " . $url);
            exit();
        }
//        $res = $this->getUserInfo($user);
//        $this->assign("res", $res);
        $this->display();
    }

    public function ajaxGetUserInfo()
    {
        $this->handleCriteriaURL();
        $wxuser = $this->user;
        $cond['openid'] = $wxuser['openid'];
        $cond['status'] = array("NEQ", 9);
//        $cond['id'] = 1;
        $user = M("jt_user")->where($cond)->find();
        $res = $this->getUserInfo($user);
        $this->ajaxReturn(["res" => $res]);
    }

    //--个人文章排行榜--
    public function ajaxWxClockUser()
    {
        $user_list = S("user_wx_clock");
        if (empty($user_list)) {
            $cond['type'] = 1;
            $cond['status'] = 1;
            $user_list = M("jt_user_clock_total")->where($cond)->order("clock desc,user_id asc")->limit(0, 100)->field("user_id, clock")->select();
            $cond_wx_max['type'] = 1;
            $cond_wx_max['clock'] = 1;
            $cond_wx_max['status'] = array("NEQ", 9);
            $wx_clock_max = M("jt_news")->where($cond_wx_max)->count();
            foreach ($user_list as &$user_wx) {
                if ($user_wx['clock'] > $wx_clock_max) {
                    $user_wx['clock'] = $wx_clock_max;
                }
            }
            S("user_wx_clock", $user_list);
        }

        $user_id_arr = [];
        foreach ($user_list as $user) {
            $user_id_arr[] = $user['user_id'];
        }
        if (!empty($user_id_arr)) {
            $cond_user['id'] = array("IN", $user_id_arr);
            $users = M("jt_user")->where($cond_user)->getField("id, name");
            foreach ($user_list as &$user) {
                if (array_key_exists($user['user_id'], $users)) {
                    $user['name'] = $users[$user['user_id']];
                }
            }
        }
        $this->ajaxReturn(["res" => $user_list]);
    }

    //--个人阅读排行榜--
    public function ajaxNbClockUser()
    {
        $user_list = S("user_nb_clock");
        if (empty($user_list)) {
            $cond['type'] = 0;
            $cond['status'] = 1;
            $user_list = M("jt_user_clock_total")->where($cond)->order("clock desc,user_id asc")->limit(0, 100)->field("user_id, clock")->select();
            $cond_nb_max['type'] = 0;
            $cond_nb_max['clock'] = 1;
            $cond_nb_max['status'] = array("NEQ", 9);
            $nb_clock_max = M("jt_news")->where($cond_nb_max)->count();
            foreach ($user_list as &$user_nb) {
                if ($user_nb['clock'] > $nb_clock_max) {
                    $user_nb['clock'] = $nb_clock_max;
                }
            }
            S("user_nb_clock", $user_list);
        }

        $user_id_arr = [];
        foreach ($user_list as $user) {
            $user_id_arr[] = $user['user_id'];
        }
        if (!empty($user_id_arr)) {
            $cond_user['id'] = array("IN", $user_id_arr);
            $users = M("jt_user")->where($cond_user)->getField("id, name");
            foreach ($user_list as &$user) {
                if (array_key_exists($user['user_id'], $users)) {
                    $user['name'] = $users[$user['user_id']];
                }
            }
        }
        $this->ajaxReturn(["res" => $user_list]);
    }

    //--公司文章排行榜--
    public function ajaxClockDept()
    {
        $type = I("get.type", 0, "");
        $dept_data_list_my = [];
        if (empty($type)) {
            $dept_data_list_my = S("dept_data_dept_clock_nb");
        } else {
            $dept_data_list_my = S("dept_data_dept_clock_wx");
        }
        if (empty($dept_data_list_my)) {
            $type = I("get.type", 0, "");
            $cond['type'] = $type;
            $cond['status'] = 1;
            $start_date = date('Y-01-01 00:00:00');
            $end_date = date("Y-m-d H:i:s");
            $cond['add_time'] = array("between", array($start_date, $end_date));
            $dept_data_list_my = array();

            $count = M("jt_dept_data")->where($cond)->count();
            $number = $count / 100;
            for ($i = 0; $i <= $number; $i++) {
                $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
                //------部门统计数据整合--------
                foreach ($dept_data_list as $dept_data) {
                    if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                        $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                    }
                    if ($dept_data['clock'] > $dept_data['number']) {
                        $dept_data['clock'] = $dept_data['number'];
                    }
                    $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                    $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
                }
            }
            //-----按打卡次数倒叙排列----
            $flag = array();
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['clock'];
            }
            array_multisort($flag, SORT_DESC, $dept_data_list_my);
            /*if (count($dept_data_list_my) > 20) {
                $dept_data_list_my = array_slice($dept_data_list_my, 0, 20);
            }*/
            //---根据截取的部门数据获取部门名称---
            $dept_id_arr = [];
            foreach ($dept_data_list_my as $dept_data_my) {
                $dept_id_arr[] = $dept_data_my['dept_id'];
            }
            if (!empty($dept_id_arr)) {
                $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
                $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
                foreach ($dept_data_list_my as &$dept_data_my) {
                    $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
                }
            }
            if (empty($type)) {
                S("dept_data_dept_clock_nb", $dept_data_list_my);
            } else {
                S("dept_data_dept_clock_wx", $dept_data_list_my);
            }
        }
        $this->ajaxReturn(["res" => $dept_data_list_my]);
    }

    //--公司文章打卡率排行榜--
    public function ajaxClockPerDept()
    {
        $type = I("get.type", 0, "");
        if (empty($type)) {
            $dept_data_list_my = S("dept_dataClockPerNb");
        } else {
            $dept_data_list_my = S("dept_dataClockPerWx");
        }

        if (empty($dept_data_list_my)) {
            $type = I("get.type", 0, "");
            $cond['type'] = $type;
            $cond['status'] = 1;
            $start_date = date('Y-01-01 00:00:00');
            $end_date = date("Y-m-d H:i:s");
            $cond['add_time'] = array("between", array($start_date, $end_date));
            $dept_data_list_my = array();

            $count = M("jt_dept_data")->where($cond)->count();
            $number = $count / 100;
            for ($i = 0; $i <= $number; $i++) {
                $dept_data_list = M("jt_dept_data")->where($cond)->field("dept_id,number,clock")->limit($i * 100, 100)->select();
                //------部门统计数据整合--------
                foreach ($dept_data_list as $dept_data) {
                    if (empty($dept_data_list_my[$dept_data['dept_id']])) {
                        $dept_data_list_my[$dept_data['dept_id']] = array("dept_id" => $dept_data['dept_id']);
                    }
                    if ($dept_data['clock'] > $dept_data['number']) {
                        $dept_data['clock'] = $dept_data['number'];
                    }
                    $dept_data_list_my[$dept_data['dept_id']]['number'] += $dept_data['number'];
                    $dept_data_list_my[$dept_data['dept_id']]['clock'] += $dept_data['clock'];
                }
            }
            //-----计算部门完成率-----
            foreach ($dept_data_list_my as &$dept_data_my) {
                if ($dept_data_my['number'] == 0) {
                    $dept_data_my['per'] = 0;
                } else {
                    $dept_data_my['per'] = number_format($dept_data_my['clock'] * 100 / $dept_data_my['number'], 2);
                    if ($dept_data_my['per'] > 100.00) {
                        $dept_data_my['per'] = 100.00;
                    }
                }
            }

            //-----按完成率倒叙排列----
            $flag = array();
            foreach ($dept_data_list_my as $dept_data_my1) {
                $flag[] = $dept_data_my1['per'];
            }
            array_multisort($flag, SORT_DESC, $dept_data_list_my);
            /*if (count($dept_data_list_my) > 20) {
                $dept_data_list_my = array_slice($dept_data_list_my, 0, 20);
            }*/
            //---根据截取的部门数据获取部门名称---

            $dept_id_arr = [];
            foreach ($dept_data_list_my as $dept_data_my_id) {
                $dept_id_arr[] = $dept_data_my_id['dept_id'];
            }
            if (!empty($dept_id_arr)) {
                $cond_dept_id_arr['id'] = array("IN", $dept_id_arr);
                $depts = M("jt_dept")->where($cond_dept_id_arr)->getField("id,name");
                foreach ($dept_data_list_my as &$dept_data_my) {
                    $dept_data_my['dept_name'] = $depts[$dept_data_my['dept_id']];
                }
            }
            if (empty($type)) {
                S("dept_dataClockPerNb", $dept_data_list_my);
            } else {
                S("dept_dataClockPerWx", $dept_data_list_my);
            }
        }
        $this->ajaxReturn(["res" => $dept_data_list_my]);

    }

    public function dept_data_update()
    {
        set_time_limit(0);
        $cond_delete['id'] = array("GT", 0);
        M("jt_dept_data")->where($cond_delete)->delete();
        M("jt_user_clock")->where($cond_delete)->delete();
        $clock_data['clock'] = 0;
        M("jt_user_clock_total")->where($cond_delete)->save($clock_data);

        $cond_news_del['status'] = array("NEQ", -1);
        M("jt_news")->where($cond_news_del)->save($clock_data);
        return;

        $cond['status'] = 1;
        $cond['name'] = array("NEQ", "集团总部");
        $depts = M("jt_dept")->where($cond)->select();
        $cond_new['status'] = array("NEQ", 9);
        $cond_new['clock'] = 1;
        $news = M("jt_news")->where($cond_new)->field("id, type")->select();
        foreach ($news as $new) {
            $data_list = [];
            foreach ($depts as $dept) {
                $data['dept_id'] = $dept['id'];
                $data['number'] = $dept['number'];
                $data['new_id'] = $new['id'];
                $data['type'] = $new['type'];
                $data_list[] = $data;
            }
            M("jt_dept_data")->addAll($data_list);
        }
    }

    public function getOpenid()
    {
        $this->user = WeixinService::getUser();
        if (empty($this->user)) {
            if (!isset($_GET['code'])) {
                $url = C('SITE_URL') . $_SERVER['REQUEST_URI'];
                $url = WeixinService::buildOauthURI($url, $this->wxuser['appid']);
                header("Location: " . $url);
                exit();
            } else {
                if (isset($_GET['code'])) {
                    WeixinService::setUser($this->wxuser['appid'], $this->wxuser['appsecret'], $_GET['code']);
                    $this->user = WeixinService::getUser();
                    if (empty($this->user)) {
                        $url = C('SITE_URL') . $_SERVER['REQUEST_URI'];
                        $url = WeixinService::buildOauthURI($url, $this->wxuser['appid']);
                        header("Location: " . $url);
                        exit();
                    }
                }
            }
        }
        $wxuser = $this->user;
        echo $wxuser['openid'];
    }

    public function getUser()
    {
        $this->handleCriteriaURL();
        $wxuser = $this->user;
        $visitor = I("get.visitor", "", "");
        $user = "";
        if (!$visitor) {
            $user = S("jt_user3" . $wxuser['openid']);
            if (empty($user)) {
                $cond['openid'] = $wxuser['openid'];
                $cond['status'] = array("NEQ", 9);
                $user = M("jt_user")->where($cond)->find();
                S("jt_user3" . $wxuser['openid'], $user);
            }
        }
        echo $user['id'] . ":" . $user['name'] . "<br>";

        //-------这个人是否在记录数据中--------
        $clock = S("user_id_clock" . $user['id']);
        echo "是否在打卡中:" . $clock . "<br>";
    }
}
